package com.csdj.service.impl;

import com.csdj.common.utils.ResponseResult;
import com.csdj.dto.CostTrendChartDto;
import com.csdj.mapper.PyBillMapper;
import com.csdj.mapper.PyBilldetailMapper;
import com.csdj.mapper.RdReadMapper;
import com.csdj.mapper.UsUserMapper;
import com.csdj.pojo.PyBilldetail;
import com.csdj.service.WorkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 涵哥
 * @date 2023-08-30 16:51
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private PyBillMapper pyBillMapper;
    @Autowired
    private RdReadMapper rdReadMapper;
    @Autowired
    private UsUserMapper usUserMapper;

    @Autowired
    private PyBilldetailMapper pyBilldetailMapper;
    /**
     * 加载本月的数据	水费、新增用户、抄表数等
     *
     * @return
     */
    @Override
    public ResponseResult loadDateForThisMonth() {
        //本月新增用户数
        Long newUserCount = usUserMapper.selectThisMonthUserCount();
        // 本月应抄表数
        Long meterCount = rdReadMapper.selectThisMonthMeterCount();
        // 本月实抄表数
        Long actualMeterCount = rdReadMapper.selectThisMonthActualMeterCount();
        // 本月用水量
        Long waterUsage = rdReadMapper.selectThisMonthwaterUsage();
        LocalDate now = LocalDate.now();
        // 本月应收水费
        double totalWaterFee = pyBillMapper.selectThisMonthTotalWaterFee(now.getYear(),now.getMonthValue());
        // 本月实收水费
        double actualWaterFee = pyBillMapper.selectThisMonthActualWaterFee(now.getYear(),now.getMonthValue());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("newUserCount",newUserCount);
        map.put("meterCount",meterCount);
        map.put("actualMeterCount",actualMeterCount);
        map.put("waterUsage",waterUsage);
        map.put("totalWaterFee",totalWaterFee);
        map.put("actualWaterFee",actualWaterFee);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult loadReceivableData(ArrayList<String> list) {
        // 日期格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月");
        ArrayList<CostTrendChartDto> costTrendChartDtos = new ArrayList<>(list.size());
        CostTrendChartDto costTrendChartDto = null;
        for (String s : list) {
            costTrendChartDto = new CostTrendChartDto();
            // 拿到年月份
            YearMonth yearMonth = YearMonth.parse(s, dateTimeFormatter);
            costTrendChartDto.setDate(s);
            // 本月应收水费
            costTrendChartDto.setTotalAmount(pyBillMapper.selectThisMonthTotalWaterFee(yearMonth.getYear(), yearMonth.getMonthValue()));
            // 本月实收水费
            costTrendChartDto.setActualAmount(pyBillMapper.selectThisMonthActualWaterFee(yearMonth.getYear(), yearMonth.getMonthValue()));
            costTrendChartDtos.add(costTrendChartDto);
        }
        return ResponseResult.okResult(costTrendChartDtos);
    }

    @Override
    public ResponseResult loadPieChartData() {
        List<PyBilldetail> pyBilldetails = pyBilldetailMapper.selectGetWaterTypeProportion();
        return ResponseResult.okResult(pyBilldetails);
    }
}
